package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Service
@Slf4j
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {


    @Autowired
    private CourseBaseService courseBaseService;

    public List<CourseTeacherDTO> queryCourseTeacher(Long courseBaseId, Long companyId) {
        //1，判断关键参数
        if (ObjectUtils.isEmpty(companyId) || ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //2.判断课程是否存在
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        if (ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        //3，判断是否同一家机构
        if (!(ObjectUtils.nullSafeEquals(companyId, courseBase.getCompanyId()))) {
            ExceptionCast.cast(ContentErrorCode.E_120020);
        }

        //6，查询数据结果
        List<CourseTeacher> list = this.list();

        //7.封装返回

        List<CourseTeacherDTO> teacherDTOList = CourseTeacherConvert.INSTANCE.pos2dtos(list);
        return teacherDTOList;
    }
/*
    //查询课程教师信息
    public CourseTeacherDTO queryCourseTeacher(Long courseId, Long companyId) {

        //判断关键数据
        if(ObjectUtils.isEmpty(companyId) || ObjectUtils.isEmpty(companyId)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        //判断课程信息是否存在
        CourseBase courseBase = courseBaseService.getById(courseId);

        if(!ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(ContentErrorCode.E_120021);
        }

        //查询此课程教师信息是否存在
        CourseTeacher courseTeacher = this.getById(courseId);

        if(!ObjectUtils.isEmpty(courseTeacher)){
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }

        //将po转为dto
        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.po2dto(courseTeacher);

        return courseTeacherDTO;
    }
*/

    //创建或修改教师信息
    @Transactional
    public CourseTeacherDTO CreateOrModifyCourseTeacher(CourseTeacherDTO courseTeacherDTO , Long companyId) {

        //判断关键数据
        if(ObjectUtils.isEmpty(courseTeacherDTO.getCourseId()) || StringUtil.isBlank(courseTeacherDTO.getPhotograph()) ||
                StringUtil.isBlank(courseTeacherDTO.getPosition()) || StringUtil.isBlank(courseTeacherDTO.getTeacherName())){

            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        //判断此课程是否存在
        CourseBase courseBase = courseBaseService.getById(courseTeacherDTO.getCourseId());

        if(ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(ContentErrorCode.E_120021);
        }

        //判断是否是同一家机构
        if(!ObjectUtils.nullSafeEquals(companyId,courseBase.getCompanyId())){
            ExceptionCast.cast(CommonErrorCode.E_403000);
        }

        //查询此课程教师信息是否存在
        CourseTeacher courseTeacher = this.getById(courseTeacherDTO.getCourseTeacherId());

        //将dto转为po
        CourseTeacher dto2po = CourseTeacherConvert.INSTANCE.dto2po(courseTeacherDTO);

        if(ObjectUtils.isEmpty(courseTeacher)){

            //信息不存在 - 新增

            boolean save = this.save(dto2po);

            //判断是否新增成功
            if(!save){
                ExceptionCast.cast(ContentErrorCode.E_1200501);
            }

        }else {

            //信息存在 - 修改
            QueryWrapper<CourseTeacher> queryWrapper = new QueryWrapper<>();

            //根据课程id
            queryWrapper.eq("courseId",courseTeacherDTO.getCourseId());

            boolean update = this.update(dto2po, queryWrapper);

            if(!update){
                ExceptionCast.cast(ContentErrorCode.E_1200501);
            }
        }

        //查询课程教师信息。创建时间
        CourseTeacher courseTeacher1 = this.getById(courseTeacherDTO.getCourseId());

        //po转dto
        CourseTeacherDTO po2dto = CourseTeacherConvert.INSTANCE.po2dto(courseTeacher1);

        //返回数据
        return po2dto;
    }

    //删除课程教师信息
    public void deleteCourseTeacher(Long courseBaseId, Long courseTeacherId) {

        //判断关键数据
        if(ObjectUtils.isEmpty(courseBaseId) || ObjectUtils.isEmpty(courseTeacherId)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        //判断此课程教师信息是否存在
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(CourseTeacher::getCourseId,courseBaseId);

        queryWrapper.eq(CourseTeacher::getId,courseTeacherId);

        CourseTeacher courseTeacher = this.getOne(queryWrapper);

        if(ObjectUtils.isEmpty(courseTeacher)){
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }

        //删除
        boolean remove = this.remove(queryWrapper);

        if(!remove){
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }


    }
}
